<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta
			name="viewport"
			content="width=device-width, initial-scale=1, maximum-scale=1"
		/>
		<title><%= title %></title>
		<meta name="renderer" content="webkit" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta
			name="viewport"
			content="width=device-width, initial-scale=1, maximum-scale=1"
		/>
		<meta name="apple-mobile-web-app-status-bar-style" content="black" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="format-detection" content="telephone=no" />

		<link rel="stylesheet" href="/layui/css/layui.css" media="all" />
	</head>
	<body>
		<form
			class="layui-form"
			action="/admin/edit"
			method="post"
			style="margin-right: 35px;"
		>
			<input type="hidden" name="id" value="<%= id %>" />
			<div class="layui-form-item" style="margin-top: 30px;">
				<label class="layui-form-label">队列名称</label>
				<div class="layui-input-inline">
					<input
						type="text"
						name="queue_name"
						lay-verify="queue_name"
						autocomplete="off"
						class="layui-input"
						value="<%= queue_name %>"
					/>
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">ES Index</label>
				<div class="layui-input-inline">
					<input
						type="text"
						name="index_name"
						lay-verify="index_name"
						autocomplete="off"
						class="layui-input"
						value="<%= index_name %>"
					/>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">淘汰策略</label>
				<div class="layui-input-block">
					<input type="radio" name="eliminate" lay-filter="eliminate"
					value="0" title="无" <% if(eliminate == 0){%>checked=""<%}%>
					value="<%= eliminate %>" /> <input type="radio"
					name="eliminate" lay-filter="eliminate" <% if(eliminate ==
					1){%>checked=""<%}%> value="1" title="按日期" /> 
					<!-- <input
					type="radio" name="eliminate" lay-filter="eliminate" <%
					if(eliminate == 2){%>checked=""<%}%> value="2"
					title="按数据量" /> -->
				</div>
			</div>
			<div class="layui-form-item" <% if(eliminate == 
				0){%>style="display: none;"<%}%>>
				<label class="layui-form-label">淘汰条件</label>
				<div class="layui-input-inline">
					<input
						type="number"
						name="eliminate_where"
						lay-verify="eliminate_where"
						placeholder=""
						autocomplete="off"
						class="layui-input"
						value="<%= eliminate_where %>"
					/>
				</div>
				<div class="layui-form-mid layui-word-aux">
					<% if(eliminate == 1){%>
						请输入过期时间，时间单位为月
					<%} else if (eliminate == 2) { %>
						数据超过多少，删除历史数
					<%}%>
				</div>
			</div>

			<div class="layui-form-item layui-form-text">
				<label class="layui-form-label">Mapping</label>
				<div class="layui-input-block">
					<textarea
						name="mapping"
						placeholder="请输入内容"
						lay-verify="mapping"
						class="layui-textarea"
						style="min-height: 230px;"
					><%= mapping %></textarea
					>
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button
						class="layui-btn"
						lay-submit=""
						lay-filter="profile"
					>
						保存
					</button>
				</div>
			</div>
		</form>
		<script src="/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript">
			/**
			 * 登录
			 *
			 * @author xyf
			 * */
			layui.use(["layer", "form"], function() {
				var layer = layui.layer,
					$ = layui.jquery,
					form = layui.form;
				//自定义验证规则
				form.verify({
					queue_name: function(value) {
						if (value.length < 1) {
							return "队列名称不能为空！";
						}
					},
					index_name: function(value) {
						if (value.length < 1) {
							return "ES Index不能为空！";
						}
					},
					eliminate_where: function(value) {
						// $('input[name=eliminate]').ch
						var eliminate = $(
							'input[name="eliminate"]:checked'
						).val();

						if (
							(eliminate == 1 || eliminate == 2) &&
							value.length < 1
						) {
							return "淘汰条件不能为空！";
						}
					},
					mapping: function(value) {
						if (value.length < 2) {
							return "Mapping 格式为json！";
						}

						if (typeof value == "string") {
							try {
								JSON.parse(value);
							} catch (e) {
								// console.log(e);
								return "格式不正确，正确格式json";
							}
						}
					}
				});
				form.on("radio(eliminate)", function(data) {
					// layer.msg(data.value);

					let pBox = $("input[name=eliminate_where]").parents(
						".layui-form-item"
					);
					if (0 == data.value) {
						$("input[name=eliminate_where]").val(0);
						pBox.find(".layui-word-aux").text("");
						pBox.hide();
					} else {
						if (1 == data.value) {
							pBox.find(".layui-word-aux").text(
								"请输入过期时间，时间单位为月"
							);
						} else {
							pBox.find(".layui-word-aux").text(
								"数据超过多少，删除历史数"
							);
						}
						pBox.show();
					}
				});
				form.on("submit(profile)", function(data) {
					function success(data) {
						if (0 == data.code) {
							layer.msg(data.msg);
							setTimeout(function() {
								var index = parent.layer.getFrameIndex(
									window.name
								); //获取窗口索引
								parent.layer.close(index);
							}, 1000);
						} else {
							layer.msg(data.msg, {
								icon: 2,
								time: 1000
							});
						}
					}
					$.post(data.form.action, data.field, success, "json");
					return false;
				});
			});
		</script>
	</body>
</html>
